home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / DEMOS / OSDEMO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-14  |  4.1 KB  |  167 lines

  1. /* $Id: osdemo.c,v 3.0 1998/02/14 18:42:29 brianp Exp $ */
  2.  
  3. /*
  4.  * Demo of off-screen Mesa rendering
  5.  *
  6.  * See Mesa/include/GL/osmesa.h for documentation of the OSMesa functions.
  7.  *
  8.  * If you want to render BIG images you'll probably have to increase
  9.  * MAX_WIDTH and MAX_HEIGHT in src/config.h.
  10.  *
  11.  * This program is in the public domain.
  12.  *
  13.  * Brian Paul
  14.  *
  15.  * PPM output provided by Joerg Schmalzl.
  16.  * ASCII PPM output added by Brian Paul.
  17.  */
  18.  
  19.  
  20. /*
  21.  * $Log: osdemo.c,v $
  22.  * Revision 3.0  1998/02/14 18:42:29  brianp
  23.  * initial rev
  24.  *
  25.  */
  26.  
  27.  
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include "GL/osmesa.h"
  32. #include "GL/glut.h"
  33.  
  34.  
  35.  
  36. #define WIDTH 400
  37. #define HEIGHT 400
  38.  
  39.  
  40.  
  41. static void render_image( void )
  42. {
  43.    GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  44.    GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  45.    GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  46.    GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  47.    GLfloat red_mat[]   = { 1.0, 0.2, 0.2, 1.0 };
  48.    GLfloat green_mat[] = { 0.2, 1.0, 0.2, 1.0 };
  49.    GLfloat blue_mat[]  = { 0.2, 0.2, 1.0, 1.0 };
  50.  
  51.  
  52.    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  53.    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  54.    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  55.    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  56.     
  57.    glEnable(GL_LIGHTING);
  58.    glEnable(GL_LIGHT0);
  59.    glEnable(GL_DEPTH_TEST);
  60.  
  61.    glMatrixMode(GL_PROJECTION);
  62.    glLoadIdentity();
  63.    glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
  64.    glMatrixMode(GL_MODELVIEW);
  65.  
  66.    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  67.  
  68.    glPushMatrix();
  69.    glRotatef(20.0, 1.0, 0.0, 0.0);
  70.  
  71.    glPushMatrix();
  72.    glTranslatef(-0.75, 0.5, 0.0); 
  73.    glRotatef(90.0, 1.0, 0.0, 0.0);
  74.    glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
  75.    glutSolidTorus(0.275, 0.85, 20, 20);
  76.    glPopMatrix();
  77.  
  78.    glPushMatrix();
  79.    glTranslatef(-0.75, -0.5, 0.0); 
  80.    glRotatef(270.0, 1.0, 0.0, 0.0);
  81.    glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
  82.    glutSolidCone(1.0, 2.0, 16, 1);
  83.    glPopMatrix();
  84.  
  85.    glPushMatrix();
  86.    glTranslatef(0.75, 0.0, -1.0); 
  87.    glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
  88.    glutSolidSphere(1.0, 20, 20);
  89.    glPopMatrix();
  90.  
  91.    glPopMatrix();
  92. }
  93.  
  94.  
  95.  
  96. int main( int argc, char *argv[] )
  97. {
  98.    OSMesaContext ctx;
  99.    void *buffer;
  100.  
  101.    /* Create an RGBA-mode context */
  102.    ctx = OSMesaCreateContext( GL_RGBA, NULL );
  103.  
  104.    /* Allocate the image buffer */
  105.    buffer = malloc( WIDTH * HEIGHT * 4 );
  106.  
  107.    /* Bind the buffer to the context and make it current */
  108.    OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT );
  109.  
  110.    render_image();
  111.  
  112.    if (argc>1) {
  113.       /* write PPM file */
  114.       FILE *f = fopen( argv[1], "w" );
  115.       if (f) {
  116.          int i, x, y;
  117.          GLubyte *ptr = (GLubyte *) buffer;
  118. #define BINARY 0
  119. #if BINARY
  120.          fprintf(f,"P6\n");
  121.          fprintf(f,"# ppm-file created by %s\n",  argv[0]);
  122.          fprintf(f,"%i %i\n", WIDTH,HEIGHT);
  123.          fprintf(f,"255\n");
  124.          fclose(f);
  125.          f = fopen( argv[1], "ab" );  /* reopen in binary append mode */
  126.          for (y=HEIGHT-1; y>=0; y--) {
  127.             for (x=0; x<WIDTH; x++) {
  128.                i = (y*WIDTH + x) * 4;
  129.                fputc(ptr[i], f);   /* write red */
  130.                fputc(ptr[i+1], f); /* write green */
  131.                fputc(ptr[i+2], f); /* write blue */
  132.             }
  133.          }
  134. #else /*ASCII*/
  135.          int counter = 0;
  136.          fprintf(f,"P3\n");
  137.          fprintf(f,"# ascii ppm file created by %s\n", argv[0]);
  138.          fprintf(f,"%i %i\n", WIDTH, HEIGHT);
  139.          fprintf(f,"255\n");
  140.          for (y=HEIGHT-1; y>=0; y--) {
  141.             for (x=0; x<WIDTH; x++) {
  142.                i = (y*WIDTH + x) * 4;
  143.                fprintf(f, " %3d %3d %3d", ptr[i], ptr[i+1], ptr[i+2]);
  144.                counter++;
  145.                if (counter % 5 == 0)
  146.                   fprintf(f, "\n");
  147.             }
  148.          }
  149. #endif
  150.          fclose(f);
  151.       }
  152.    }
  153.    else {
  154.       printf("Specify a filename if you want to make a ppm file\n");
  155.    }
  156.  
  157.    printf("all done\n");
  158.  
  159.    /* free the image buffer */
  160.    free( buffer );
  161.  
  162.    /* destroy the context */
  163.    OSMesaDestroyContext( ctx );
  164.  
  165.    return 0;
  166. }
  167.